package com.shiyi.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import com.shiyi.core.utils.result.Result;
import com.shiyi.system.annotation.NoPermission;
import com.shiyi.system.annotation.OperaLog;
import com.shiyi.system.entity.SysUser;
import com.shiyi.system.redis.RedisCache;
import com.shiyi.system.service.SysUserService;
import com.shiyi.system.utils.EntityConstant;
import com.shiyi.system.utils.UserManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shiyi
 * @since 2020-11-19
 */
@Api(value ="用户管理",tags = "用户管理")
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private SysUserService service;

    @Autowired
    private UserManager userManager;

    @Autowired
    private RedisCache redisCache;


/*    *//**
     * 根据用户名查询用户信息,用户集成SpringSecurity，目前暂不适用
     *//*
    @ApiOperation(value = "根据用户名查询用户信息,用户集成SpringSecurity，目前暂不适用")
    @RequestMapping("/loadUserByUsername")
    public Result<User> loadUserByUsername(@RequestParam("userName") String userName) {
        User user = sysUserService.loadUserByUsername(userName);
        System.out.println(user);
        return Result.success(user);
    }*/


    /**
     * 新增用户信息
     */
    @OperaLog(operaModule = "用户管理", type = 1)
    @ApiOperation(value = "新增用户信息")
    @PostMapping("/insert")
    public Result<Object> insert(@RequestBody @Valid SysUser user, BindingResult res) {
        if (res.hasErrors()) {
            return Result.BindingError(res);
        }
        if (!service.queryUserByUserName(user.getUserName())) {
            return Result.fail(EntityConstant.EXIST_NAME);
        }
        return service.insertUser(user) ? Result.success() : Result.fail();
    }

    /**
     * 修改用户信息
     */
    @OperaLog(operaModule = "用户管理", type = 2)
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody @Valid SysUser user, BindingResult res) {
        if (res.hasErrors()) {
            return Result.BindingError(res);
        }
        if (!service.queryUserByIdAndName(user)) {
            return Result.fail(EntityConstant.EXIST_NAME);
        }
        return service.updateUser(user) ? Result.success() : Result.fail();
    }

    /**
     * 查询所有用户
     */
    @ApiOperation(value = "查询用户信息")
    @OperaLog(operaModule = "用户管理", type = 6)
    @GetMapping("/listUser")
    public Result<Object> listUser(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> userList = redisCache.getCacheList("userList");
        if (userList == null || userList.size() == 0) {
            userList = service.listUser();
            redisCache.setCacheList("userList", userList);
        }
//       List<SysUser> userList = service.listUser();
        PageInfo<SysUser> listPageInfo = new PageInfo<>(userList);
        return Result.success(listPageInfo);
    }


    @OperaLog(operaModule = "用户管理", type = 3)
    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(paramType = "int")
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable String id) {
        return service.deleteUser(id) ? Result.success() : Result.fail();
    }


    /**
     * 账号密码登录
     */
    @NoPermission
    @OperaLog(operaModule = "登录", type = 4)
    @PostMapping("/userLogin")
    public Result<Object> userLogin(@RequestParam String username, @RequestParam String password) {
        if (StringUtils.isNullOrEmpty(username) || StringUtils.isNullOrEmpty(password)) {
            return Result.fail(EntityConstant.USERNAME_OR_PASSWORD_IS_NULL);
        }
        return service.userLogin(username, password);
    }

    /**
     * 退出登录
     */
    @NoPermission
    @OperaLog(operaModule = "退出登录", type = 5)
    @DeleteMapping("/loginOut")
    public Result<Object> loginOut() {
        return Result.success();
    }

}

